package demo5;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-07-31
 * Time: 18:47
 */
public class Test {

    public static void main(String[] args){
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();

        //消费者
        Thread customer = new Thread(()->{
            while(true){
                try {
                    int value = queue.take();
                    System.out.println("消费了 ->" + value);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        customer.start();

        //作为生产者
        Thread producer = new Thread(()->{
            int n = 0;
            while(true){
                try {
                    queue.put(n);
                    System.out.println("生产了 ->" + n);
                    n++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        producer.start();
    }

}
